/**
 * @author <a href="mailto:enjie.xu@yunhutech.com">xiaoming</a>
 * @since 2021/6/11
 */
public class MyThread extends Thread{
    volatile boolean stop = false;

    @Override
    public void run() {
        while (!stop){
            System.out.println(getName()+" is running");
            try {
               sleep(1000);
            } catch (Exception e) {
                System.out.println(e);
                System.out.println("week up from block");
                stop = true;
            }
        }
        System.out.println(getName() + "is exiting");
    }
}

class Demo3{
    public static void main(String[] args) throws InterruptedException {
        MyThread m = new MyThread();
        System.out.println("Start thread");
        m.start();

        Thread.sleep(3000);
        System.out.println("Interrupt thread"+m.getName());
        m.stop = true;//设置共享变量为true
        m.interrupt();//阻塞时退出阻塞状态
        Thread.sleep(3000);

        System.out.println("Stoping application");
    }
}
